package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_RECOVER_ENERGY,desc = "使用道具恢复体力")
public class DouLuoDaLuRecoverEnergyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInLieHunPeriod()) != 0) return;

        DouLuoDaLuProto.DouLuoDaLuRecoverEnergyReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuRecoverEnergyReqMsg.parseFrom(packet.getBytes());
        int num = reqMsg.getNum();
        if(num < 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        Property property = PropertyHelper.parseStringToProperty(DouLuoDaLuMgr.getConfig().ENERGY_ITEM_ID + "=" + num);
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
            ret = GameErrorCode.E_DOU_LUO_DA_LU_ENERGY_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(property);
        DouLuoDaLuProto.DouLuoDaLuRecoverEnergyCrossReqMsg.Builder crossReqMsg = DouLuoDaLuProto.DouLuoDaLuRecoverEnergyCrossReqMsg.newBuilder();
        crossReqMsg.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        crossReqMsg.setNum(num);
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_RECOVER_ENERGY, crossReqMsg, crossReqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
